package com.woniuxy.ptapp.app.controller;
import com.woniuxy.ptapp.common.xxljob.createJob.XxlJobCreateParam;
import com.woniuxy.ptapp.common.xxljob.createJob.XxlJobManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
public class OrderCreateController {

    @Resource
    private XxlJobManager xxlJobCreator;

    @PostMapping("order")
    public String createOrder(){

        System.out.println("开始执行动态测试");
        System.out.println(xxlJobCreator);
        //创建一个延迟任务
        XxlJobCreateParam param = new XxlJobCreateParam();
        param.setExecutorId(4);
        param.setJobDesc("动态测试4");
        param.setScheduleConf(dateToCron(LocalDateTime.now().plusSeconds(10)));
        param.setExecutorHandler("test");
        int jobId = xxlJobCreator.createJob(param);

        return "success";
    }

    @GetMapping("/pay")
    public void payOrder(){
        //TODO 支付完成

        xxlJobCreator.removeJob(7);
    }

    public String dateToCron(LocalDateTime date){
        String cronFormat="ss mm HH dd MM ? yyyy";
        return DateTimeFormatter.ofPattern(cronFormat).format(date);
    }


}
